package com.jin.webvideo.config.filter.access.rule;

import com.google.common.cache.Cache;
import com.jin.webvideo.base.SpringBeanGetter;
import com.jin.webvideo.config.filter.access.GlobalStore;
import com.jin.webvideo.entity.SessionStore;
import com.jin.webvideo.service.AccessServer;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
@Data
public class ForbidInetAddress implements ForbidHandler{
    private AccessServer accessServer;
    // 封禁时长
    private long time;
    // 提示信息
    private String information;
    private String key;

    public ForbidInetAddress(long time, String information, String key) {
        this.accessServer = SpringBeanGetter.getBean(AccessServer.class);
        this.time = time;
        this.information = information;
        this.key = key;
    }

    public ForbidInetAddress() {
        this.accessServer = SpringBeanGetter.getBean(AccessServer.class);
    }

    @Override
    public String name() {
        return "ForbidInetAddress";
    }

    @Override
    public void handle(GlobalStore globalStore, SessionStore store, HttpServletRequest request) {
        String address = request.getRemoteAddr();
        if (this.accessServer == null) {
            return;
        }
        accessServer.forbidAddress(address, time, information);
        if (key == null || store == null) {
            return;
        }
        Cache cache = globalStore.get(key);
        if (cache != null) {
            cache.invalidate(address);
        }

    }
}
